package com.zwps.biz.domain.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zwps.biz.dal.db.dao.FileRecordBizSettingDAO;
import com.zwps.biz.dal.db.object.FileRecordBizSettingDO;
import com.zwps.biz.domain.converter.FileRecordBizSettingConverter;
import com.zwps.biz.domain.service.FileRecordBizSettingService;
import com.zwps.biz.api.model.dto.file.record.biz.setting.AddFileRecordBizSettingDTO;
import com.zwps.biz.api.model.dto.file.record.biz.setting.SelectFileRecordBizSettingDTO;
import com.zwps.biz.api.model.dto.file.record.biz.setting.UpdateFileRecordBizSettingDTO;
import com.zwps.biz.api.model.vo.file.record.biz.setting.SelectFileRecordBizSettingPageVO;
import com.zwps.common.api.model.vo.PageInfoVO;
import com.zwps.common.core.exception.UserActionException;
import com.zwps.common.core.service.CommonService;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author shanwen
 * @description:
 * @date 2023-09-15
 * @ClassName FileRecordBizSettingServiceImpl
 */
@Service
@AllArgsConstructor
public class FileRecordBizSettingServiceImpl implements FileRecordBizSettingService, CommonService {

    private FileRecordBizSettingDAO fileRecordBizSettingDAO;

    @Override
    public void updateFileRecordBizSetting(String id, UpdateFileRecordBizSettingDTO dto) {
        FileRecordBizSettingDO fileRecordBizSettingDO = fileRecordBizSettingDAO.selectById(id);
        if (fileRecordBizSettingDO == null) {
            throw new UserActionException("当前修改的业务表文件记录设置不存在");
        }

        LambdaQueryWrapper<FileRecordBizSettingDO> wrapper = new QueryWrapper<FileRecordBizSettingDO>().lambda();
        wrapper.eq(FileRecordBizSettingDO::getBizTable,dto.getBizTable());
        Long count = fileRecordBizSettingDAO.selectCount(wrapper);
        if (count > 0 && !dto.getBizTable().equals(fileRecordBizSettingDO.getBizTable())) {
            throw new UserActionException("该业务表已存在，修改业务表文件记录设置失败");
        }

        LambdaUpdateWrapper<FileRecordBizSettingDO> update = Wrappers.lambdaUpdate(FileRecordBizSettingDO.class);
        update.set(change(dto.getBizTable(), fileRecordBizSettingDO.getBizTable()), FileRecordBizSettingDO::getBizTable, dto.getBizTable());
        update.set(change(dto.getBizTableName(), fileRecordBizSettingDO.getBizTableName()), FileRecordBizSettingDO::getBizTableName, dto.getBizTableName());
        update.set(change(dto.getVideoCompress(), fileRecordBizSettingDO.getVideoCompress()), FileRecordBizSettingDO::getVideoCompress, dto.getVideoCompress());
        update.set(change(dto.getVideoCover(), fileRecordBizSettingDO.getVideoCover()), FileRecordBizSettingDO::getVideoCover, dto.getVideoCover());
        update.set(change(dto.getVideoCoverCaptureStrategy(), fileRecordBizSettingDO.getVideoCoverCaptureStrategy()), FileRecordBizSettingDO::getVideoCoverCaptureStrategy, dto.getVideoCoverCaptureStrategy());
        update.set(change(dto.getVideoCoverCaptureWidth(), fileRecordBizSettingDO.getVideoCoverCaptureWidth()), FileRecordBizSettingDO::getVideoCoverCaptureWidth, dto.getVideoCoverCaptureWidth());
        update.set(change(dto.getVideoCoverCaptureHeight(), fileRecordBizSettingDO.getVideoCoverCaptureHeight()), FileRecordBizSettingDO::getVideoCoverCaptureHeight, dto.getVideoCoverCaptureHeight());
        update.set(change(dto.getPictureCompress(), fileRecordBizSettingDO.getPictureCompress()), FileRecordBizSettingDO::getPictureCompress, dto.getPictureCompress());
        update.set(change(dto.getPictureCompressWidth(), fileRecordBizSettingDO.getPictureCompressWidth()), FileRecordBizSettingDO::getPictureCompressWidth, dto.getPictureCompressWidth());
        update.set(change(dto.getPictureCompressHeight(), fileRecordBizSettingDO.getPictureCompressHeight()), FileRecordBizSettingDO::getPictureCompressHeight, dto.getPictureCompressHeight());
        update.set(change(dto.getPictureCompressWatermark(), fileRecordBizSettingDO.getPictureCompressWatermark()), FileRecordBizSettingDO::getPictureCompressWatermark, dto.getPictureCompressWatermark());
        update.set(change(dto.getWatermarkText(), fileRecordBizSettingDO.getWatermarkText()), FileRecordBizSettingDO::getWatermarkText, dto.getWatermarkText());

        if (StrUtil.isNotEmpty(update.getSqlSet())) {
            update.eq(FileRecordBizSettingDO::getId, id);
            if (fileRecordBizSettingDAO.update(null, update) < 1) {
                throw new UserActionException("修改业务表文件记录设置失败");
            }
        }
    }

    @Override
    public PageInfoVO<SelectFileRecordBizSettingPageVO> selectFileRecordBizSettingPage(SelectFileRecordBizSettingDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<FileRecordBizSettingDO> wrapper = new QueryWrapper<FileRecordBizSettingDO>().lambda();
        wrapper.like(StringUtils.isNotBlank(dto.getBizTable()), FileRecordBizSettingDO::getBizTable, dto.getBizTable());
        wrapper.like(StringUtils.isNotBlank(dto.getBizTableName()), FileRecordBizSettingDO::getBizTableName, dto.getBizTableName());
        List<FileRecordBizSettingDO> list = fileRecordBizSettingDAO.selectList(wrapper);

        ArrayList<SelectFileRecordBizSettingPageVO> voList = new ArrayList<>();
        Page<FileRecordBizSettingDO> page = (Page<FileRecordBizSettingDO>) list;
        PageInfoVO<SelectFileRecordBizSettingPageVO> pageInfo = new PageInfoVO<>(page.getPageNum(), page.getPageSize(), page.getPages(), page.getTotal(),
                voList);

        for (FileRecordBizSettingDO fileRecordBizSettingDO : list) {
            SelectFileRecordBizSettingPageVO vo = FileRecordBizSettingConverter.INSTANCE.toSelectFileRecordBizSettingPageVO(fileRecordBizSettingDO);
            voList.add(vo);
        }

        return pageInfo;
    }

    @Override
    public void addFileRecordBizSetting(AddFileRecordBizSettingDTO dto) {
        LambdaQueryWrapper<FileRecordBizSettingDO> wrapper = new QueryWrapper<FileRecordBizSettingDO>().lambda();
        wrapper.eq(FileRecordBizSettingDO::getBizTable,dto.getBizTable());
        Long count = fileRecordBizSettingDAO.selectCount(wrapper);
        if (count > 0) {
            throw new UserActionException("该业务表已存在，新增业务表文件记录设置失败");
        }

        FileRecordBizSettingDO fileRecordBizSettingDO = FileRecordBizSettingConverter.INSTANCE.toFileRecordBizSettingDO(dto);
        if (fileRecordBizSettingDAO.insert(fileRecordBizSettingDO) < 1) {
            throw new UserActionException("新增业务表文件记录设置失败");
        }
    }

    @Override
    public void deleteFileRecordBizSetting(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        int result = fileRecordBizSettingDAO.deleteBatchIds(idList);
        if (result != idList.size()) {
            long count = fileRecordBizSettingDAO
                    .selectCount(Wrappers.lambdaQuery(FileRecordBizSettingDO.class).in(FileRecordBizSettingDO::getId, idList));
            if (count > 0) {
                throw new UserActionException("删除业务表文件记录设置失败");
            }
        }
    }
}
